import * as soundAlbumModel from '../../models/sound_album';
import * as soundVoiceModel from '../../models/sound_voice';
import * as soundCategoryModel from '../../models/sound_category';
import * as soundCategoryRelationModel from '../../models/sound_category_relation';
import { ValidException, EmptyException, SystemException } from '../../helps/exception';

export function* listAlbum({ page, number, order, direction }) {
  return yield soundAlbumModel.list(page, number, order, direction);
}

export function* addAlbum({ data }) {
  const album = yield soundAlbumModel.add(data);
  if (!album) throw SystemException('操作失败');
  return { id: album.id };
}

export function* getAlbum({ album_id }) {
  if (!album_id) throw ValidException('id 不能为空');
  const album = yield soundAlbumModel.get(album_id);
  if (!album) throw EmptyException('无相关信息');
  return album;
}

export function* putAlbum({ album_id, data }) {
  if (!album_id) throw ValidException('id 不能为空');
  const b = yield soundAlbumModel.put(album_id, data);
  if (!b) throw SystemException('操作失败');
  return { id: album_id };
}

export function* delAlbum({ album_id }) {
  if (!album_id) throw ValidException('id 不能为空');
  const b = yield soundAlbumModel.del(album_id);
  if (!b) throw SystemException('操作失败');
  return { id: album_id };
}

export function* listVoice({ album_id, page, number, order, direction }) {
  if (!album_id) throw ValidException('id 不能为空');
  return yield soundVoiceModel.listByAlbum(album_id, page, number, order, direction);
}

export function* addVoice({ album_id, data }) {
  if (!album_id) throw ValidException('id 不能为空');
  const album = yield soundVoiceModel.add({ album_id, ...data });
  if (!album) throw SystemException('操作失败');
  return { id: album.id };
}

export function* getVoice({ voice_id }) {
  if (!voice_id) throw ValidException('id 不能为空');
  const voice = yield soundVoiceModel.get(voice_id);
  if (!voice) throw EmptyException('无相关信息');
  return voice;
}

export function* putVoice({ voice_id, data }) {
  if (!voice_id) throw ValidException('id 不能为空');
  const b = yield soundVoiceModel.put(voice_id, data);
  if (!b) throw SystemException('操作失败');
  return { id: voice_id };
}

export function* delVoice({ voice_id }) {
  if (!voice_id) throw ValidException('id 不能为空');
  const b = yield soundVoiceModel.del(voice_id);
  if (!b) throw SystemException('操作失败');
  return { id: voice_id };
}

export function* listCategory({ page, number, order, direction }) {
  return yield soundCategoryModel.list(page, number, order, direction);
}

export function* addCategory({ data }) {
  const category = yield soundCategoryModel.add(data);
  if (!category) throw SystemException('操作失败');
  return { id: category.id };
}

export function* getCategory({ category_id }) {
  if (!category_id) throw ValidException('id 不能为空');
  const category = yield soundCategoryModel.get(category_id);
  if (!category) throw EmptyException('无相关信息');
  return category;
}

export function* putCategory({ category_id, data }) {
  if (!category_id) throw ValidException('id 不能为空');
  const b = yield soundCategoryModel.put(category_id, data);
  if (!b) throw SystemException('操作失败');
  return { id: category_id };
}

export function* delCategory({ category_id }) {
  if (!category_id) throw ValidException('id 不能为空');
  const b = yield soundCategoryModel.del(category_id);
  if (!b) throw SystemException('操作失败');
  return { id: category_id };
}

export function* listAlbumCategory({ album_id }) {
  if (!album_id) throw ValidException('id 不能为空');
  return yield soundCategoryRelationModel.listByAlbum(album_id);
}

export function* addAlbumCategory({ album_id, category_id }) {
  if (!album_id || !category_id) throw ValidException('id 不能为空');
  const album = yield soundCategoryRelationModel.add({ album_id, category_id });
  if (!album) throw SystemException('操作失败');
  return { id: album.id };
}

export function* delAlbumCategory({ album_id, relation_id }) {
  if (!album_id || !relation_id) throw ValidException('id 不能为空');
  const b = yield soundCategoryRelationModel.del(relation_id);
  if (!b) throw SystemException('操作失败');
  return { id: relation_id };
}
